<template>
  <div>
    <el-dialog
      :title="title"
      v-model="visable"
      width="45%"
      style="max-width: 550px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 80%;padding-left: 10%;"
        >
          <el-form-item label="角色名称:" prop="roleName">
            <el-input v-model="dialogFormData.roleName" placeholder="请输入角色名称" clearable />
          </el-form-item>
          <el-form-item label="父角色:" prop="parentRoleId">
            <el-tree-select
              v-model="dialogFormData.parentRoleId"
              :data="roleData"
              filterable
              :props="roleTreeProps"
              :check-strictly="true"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注:" prop="remark">
            <el-input v-model="dialogFormData.remark" type="textarea" placeholder="请输入角色备注" clearable />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 10%;">
          <el-button  size="small" @click="visable = false"> 取消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listAllRole, addRole,updateRole, getRoleById } from '@/api/system/role'
// 查询条件的form表单原始数据(修改和新增共用一个页面弹窗时(不共用无错),this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgFormData = {
        roleId: '',
        roleName: '',
        parentRoleId:'',
        remark:''
      }
export default {
  data() {
    return {
      title:"",
      visable: false,
      dialogFormData: {...orgFormData},
      dialogFormRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] },
          { min: 3, max: 20, message: '3~20字符以内', trigger: ['blur', 'change'] }
        ]
      },
      roleData:[],
      roleTreeProps: {
        children: 'children',
        label: 'roleName',
        value: 'roleId'
      }
      
    }
  },
  methods: {
    openModal(roleId) {
      this.visable = true
      this.$nextTick(()=>{
        this.dialogFormData = {...orgFormData}
      })
      if (this.isNotBlank(roleId)) {
        this.title = '编辑角色'
        // 获取修改的角色信息
        getRoleById({ 'roleId': roleId }).then(res => {
          if (res.code === 1) {
            this.dialogFormData.roleId = res.data.roleId
            this.dialogFormData.roleName = res.data.roleName
            this.dialogFormData.rightsIdList = res.data.rightsIdList
            this.dialogFormData.roleStatus = res.data.roleStatus
            this.dialogFormData.parentRoleId = res.data.parentRoleId
            this.dialogFormData.remark = res.data.remark
          }
        })
      } else {
        this.title = '新增角色'
      }
      this.$nextTick(() => {
        // 获取所有角色
        listAllRole({}).then(res=>{
          if (res.code==1) {
            this.roleData = res.data
            this.roleData.unshift({'roleId':'0','parentRoleId':'0','roleName':'无','children':[]})
          }
        })
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.roleId)) {
            addRole(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateRole(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  /deep/.el-form-item__content {
    display: inline-block;
  }
  /deep/.el-drawer__title {
    text-align: left;
  }
</style>
